
# include "dfxPlugin.h"
# include "dfxLog.h"

# if DFX_PLATFORM == DFX_WINDOWS
    # include <Windows.h>
# endif

namespace DFX
{
    DFXRESULT LoadDLL(const String &dllFile, void *handle)
    {
# if DFX_PLATFORM == DFX_WINDOWS
        HMODULE hDLL = LoadLibrary(dllFile.c_str());

        if (!hDLL)
        {
            DFX_ERROR("Unable to load dll: %s", dllFile.c_str());
            return DFX_LOADDLL;
        }
        else
        {
            *((HMODULE*)handle) = hDLL;
        }

        return DFX_SUCCESS;
# else
        return DFX_LOADDLL;
# endif
    }

    DFXRESULT UnloadDLL(void *handle)
    {
# if DFX_PLATFORM == DFX_WINDOWS
        if (handle)
        {
            HMODULE hDLL = *((HMODULE*)handle);

            if (!FreeLibrary(hDLL))            
                DFX_WARNING( "Unable to free library");                            
        }
# endif

        return DFX_SUCCESS;
    }

    void* GetFunction(const String &funcName, void *handle)
    {
        void *funcAddr = NULL;

# if DFX_PLATFORM == DFX_WINDOWS
        if (handle)
            funcAddr = (void*)GetProcAddress(*((HMODULE*)handle), funcName.c_str());
# endif

        return funcAddr;
    }
}